package cn.wolfcode.p2p.hosting.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.business.domain.RechargeOnline;
import cn.wolfcode.p2p.hosting.service.IHostingService;
import cn.wolfcode.p2p.hosting.util.*;
import cn.wolfcode.p2p.util.Constants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.RoundingMode;
import java.util.Map;

@Service@Transactional
public class HostingServiceImpl implements IHostingService {

        @Override
        public void createAccount(LoginInfo loginInfo) {
                Map<String, String> params = HostingUtil.getBaseParams();
                params.put("service","create_activate_member");
                //设置业务参数
                params.put("identity_id",HostingUtil.getHostingIdentityId(loginInfo.getId()));
                params.put("identity_type","UID");
                params.put("member_type","1");
                params.put("client_ip","192.168.10.203");
                HostingUtil.requestHandler(params,true);
        }

        /**
         * 绑定账户手机
         * @param loginInfo
         */
        @Override
        public void bindPhone(LoginInfo loginInfo) {
                Map<String, String> params = HostingUtil.getBaseParams();
                params.put("service","binding_verify");
                //设置业务参数
                params.put("identity_id",HostingUtil.getHostingIdentityId(loginInfo.getId()));
                params.put("identity_type","UID");
                params.put("verify_type","MOBILE");
                try {
                        byte[] bytes = RSA.encryptByPublicKey(loginInfo.getUsername().getBytes(HostingUtil.INPUT_CHARSET),HostingUtil.ENCODE_PULIC_KEY);
                        String encode = Base64.encode(bytes);
                        params.put("verify_entity",encode);
                        params.put("client_ip","192.168.10.203");
                        HostingUtil.requestHandler(params,true);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        @Override
        public String hostingPersonal(LoginInfo loginInfo) {
                Map<String, String> params = HostingUtil.getBaseParams();
                params.put("service","show_member_infos_sina");
                //设置业务参数
                params.put("identity_id",HostingUtil.getHostingIdentityId(loginInfo.getId()));
                params.put("identity_type","UID");
                params.put("resp_method","1");
                params.put("return_url","http://localhost/personal");
                Map<String, String> map = HostingUtil.requestHandler(params,true);
                return map.get("redirect_url");
        }

        @Override
        public void bindReal(RealAuth realAuth, LoginInfo loginInfo) {
                Map<String, String> params = HostingUtil.getBaseParams();
                params.put("service","set_real_name");
                //设置业务参数
                params.put("identity_id",HostingUtil.getHostingIdentityId(loginInfo.getId()));
                params.put("identity_type","UID");
                try {
                        byte[] bytes = RSA.encryptByPublicKey(realAuth.getRealname().getBytes(HostingUtil.INPUT_CHARSET), HostingUtil.ENCODE_PULIC_KEY);
                        String encode = Base64.encode(bytes);
                        params.put("real_name",encode);
                        params.put("cert_type","IC");
                         bytes = RSA.encryptByPublicKey(realAuth.getIdNumber().getBytes(HostingUtil.INPUT_CHARSET), HostingUtil.ENCODE_PULIC_KEY);
                        String encode1 = Base64.encode(bytes);
                        params.put("cert_no",encode1);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                Map<String, String> map = HostingUtil.requestHandler(params,true);
        }

        @Override
        public String hostingSetPayPassword(LoginInfo loginInfo) {
                Map<String, String> params = HostingUtil.getBaseParams();
                params.put("service","set_pay_password");
                //设置业务参数
                params.put("identity_id",HostingUtil.getHostingIdentityId(loginInfo.getId()));
                params.put("identity_type","UID");
                params.put("return_url","http://localhost/callBackSetPayPassword?uid="+loginInfo.getId());
                params.put("client_ip","192.168.10.203");
                Map<String, String> map = HostingUtil.requestHandler(params,true);
                return map.get("redirect_url");
        }

        @Override
        public String hostingRecharge(RechargeOnline ro) {
                Map<String, String> params = HostingUtil.getBaseParams();
                params.put("service","create_hosting_deposit");
                //设置业务参数
                params.put("identity_id",HostingUtil.getHostingIdentityId(ro.getApplier().getId()));
                params.put("identity_type","UID");
                params.put("out_trade_no",ro.getTradeCode());
                params.put("payer_ip","192.168.10.203");
                String amount1 = ro.getAmount().setScale(Constants.SCALE_SHOW, RoundingMode.HALF_UP).toString();
                params.put("pay_method","online_bank^"+amount1+"^SINAPAY");
                params.put("amount",amount1);
                params.put("return_url","http://localhost/callBackRecharge?tradeCode="+ro.getTradeCode());
                params.put("notify_url","http://localhost/callBackRechargeResult");
                String s = HostingUtil.requestHandlerToString(params, false);
                /*return null;*/
                return s;
        }
}

